package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerComment;
import com.ian.service.PerCommentService;
import com.ian.service.PerStallService;
import com.ian.service.PerUserService;
import com.ian.vo.UserCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 评论控制层
 */
@Api("评论相关API")
@RestController
public class PerCommentController {
    @Autowired
    PerCommentService perCommentService;

    @Autowired
    PerUserService perUserService;

    @Autowired
    PerStallService perStallService;

    @ApiOperation("评论列表")
    @PostMapping(value = "/comments", produces = "application/json;charset=UTF-8")
    public ResultMsg comments(@RequestParam("stallId1") int stallId1, @RequestParam("userId") int userId) {
        List<UserCommentVo> vos = new ArrayList<>();
        List<PerComment> comments = perCommentService.findByStallId(stallId1);
        System.out.println(comments.toString());
        for (PerComment comment : comments) {
            UserCommentVo vo = new UserCommentVo();
            vo.setUsername(perUserService.selectByPrimaryKey(userId).getPerUserName());
            vo.setDetails(comment.getPerCommentDetails());
            vo.setHeadImg(perUserService.selectByPrimaryKey(userId).getPerUserHeadimg());
            vo.setTime(comment.getPerCommentTime());
            vos.add(vo);
        }

        return ResultMsg.success("查询评论成功~", vos);
    }

    @ApiOperation("发表评论")
    @PostMapping(value = "/pushCmt", produces = "application/json;charset=UTF-8")
    public ResultMsg pushCmt(@RequestParam("stallId") int stallId, @RequestParam("details") String details) {
        PerComment perComment = new PerComment();
        perComment.setPerCommentDetails(details);
        perComment.setPerStallId(stallId);
        perComment.setPerUserId(perStallService.selectByPrimaryKey(stallId).getPerUserId());
        perComment.setPerCommentTime(new Date());
        perCommentService.insert(perComment);
        return ResultMsg.success("发表成功~", stallId);
    }
}
